package com.namecard.namecardapi.controller;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.namecard.namecardapi.entity.TContact;
import com.namecard.namecardapi.entity.vo.AddTContactVo;
import com.namecard.namecardapi.entity.vo.SaveTContactVo;
import com.namecard.namecardapi.result.Result;
import com.namecard.namecardapi.service.TContactService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@DS("slave2")
@Api(tags = "联系方式") //Swagger生成api的提示
@RestController
@RequestMapping("/admin/system/t-contact")
public class TContactController {

    @Resource
    private TContactService tContactService;


    /**
     * 查询所有
     * @return
     */
    @ApiOperation("查询所有")
    @GetMapping("/all")
    public Result all(){
        // 创建 QueryWrapper 对象
        QueryWrapper<TContact> queryWrapper = new QueryWrapper<>();
        // 按照 id 倒序排序
        queryWrapper.orderByDesc("id");

        // 使用 QueryWrapper 进行查询
        List<TContact> list = tContactService.list(queryWrapper);
        return Result.ok(list);
    }

    /**
     * 根据id查询
     * @return
     */
    @ApiOperation("根据id查询")
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable Long id){
        TContact contact = tContactService.getById(id);
        return Result.ok(contact);
    }


    /**
     * 根据id删除
     * @param id
     * @return
     */
    @ApiOperation("根据id删除")
    @DeleteMapping("/del/{id}")
    public Result delById(@PathVariable Long id){
        boolean b = tContactService.removeById(id);
        return Result.ok("删除成功");
    }

    /**
     * 修改
     * @param saveTContactVo
     * @return
     */
    @ApiOperation("根据id修改")
    @PutMapping("/update")
    public Result update(@RequestBody SaveTContactVo saveTContactVo){
        TContact tContact = tContactService.getById(saveTContactVo.getId());
        if (tContact==null){
            return Result.fail("没有该联系方式");
        }
        tContact.setUpdateTime(new Date());
        tContact.setContactPerson(saveTContactVo.getContactPerson());
        tContact.setContactWay(saveTContactVo.getContactWay());
        tContact.setContactNum(saveTContactVo.getContactNum());
        boolean b = tContactService.updateById(tContact);
        return b ? Result.ok("修改成功") : Result.fail("修改失败");
    }


    /**
     * 添加联系
     * @param addTContactVo
     * @return
     */
    @ApiOperation("添加联系方式")
    @PostMapping("/save")
    public Result save(@RequestBody AddTContactVo addTContactVo){
        TContact contact = new TContact();
        contact.setContactNum(addTContactVo.getContactNum());
        contact.setContactWay(addTContactVo.getContactWay());
        contact.setContactPerson(addTContactVo.getContactPerson());
        contact.setCreateTime(new Date());
        contact.setUpdateTime(new Date());
        boolean b = tContactService.save(contact);
        return b ? Result.ok("添加成功") : Result.fail("添加失败");
    }

}
